#include "PreCompile.hpp"
#include "Task.hpp"
#include "LibBuildNGInit.hpp"

namespace BuildNG
{
  ///////////
  // TaskSequence
  TaskTypesRegistry task_types_registry;

  TaskSequence::TaskSequence()
  {
    assert_libbuildng_initialized();
  }

  void TaskSequence::load_from_xml(const Xml::Element &xml)
  {
    Xml::ConstElementList children = xml.get_child_elements();
    for(int i = 0;i < (int)children.size();i++)
    {
      const Xml::Element *elem = children[i];
      assert(elem);
      
      Glib::RefPtr<TaskTypeInfo> type_info = task_types_registry.get_by_xml_tag_name(elem->get_name());
      if(!type_info)
      {
        throw Xml::ContentException(*elem,"Unknown task type: \"" + elem->get_name() + "\".");
      }
      
      Glib::RefPtr<Task> cur_task = type_info->create_instance();
      cur_task->load_from_xml(*elem);
      tasks.push_back(cur_task);
    }
  }

  void TaskSequence::run(BuildContext &context) const
  {
    for(int i = 0;i < (int)tasks.size();i++)
    {
      tasks[i]->run(context);
    }
  }

  /////////
  // Task
  Task::Task()
  {
    assert_libbuildng_initialized();
  }
}

